//
//  InfoStore.m
//  WonderfulNotes
//
//  Created by 千锋 on 16/1/13.
//  Copyright © 2016年 andy. All rights reserved.
//

#import "InfoStore.h"

@interface InfoStore ()

@property(nonatomic,strong)NSMutableDictionary * dic;

//获取沙河路径
-(NSString *)infoPathForKey:(NSString *)key;

@end

@implementation InfoStore

+(instancetype)sharedStore{
   
    static InfoStore * shareManager = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        shareManager = [[self alloc] initPrivate];
        
    });
    return shareManager;
}

-(instancetype)initPrivate{
    
   self = [super init];
    
    if (self) {
        
    _dic = [[NSMutableDictionary alloc]init];
        //注册为低内存通知的观察者
        NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
        [nc addObserver:self
               selector:@selector(clearCaches:)
                   name:UIApplicationDidReceiveMemoryWarningNotification
                 object:nil];
    }
    return self;
}

-(void)setName:(NSString *)name forKey:(NSString *)key{
    
    [self.dic setObject:name forKey:key];
    //获取沙河路径
    NSString *path = [self infoPathForKey:key];
    NSData * data = [name dataUsingEncoding:NSUTF8StringEncoding];
    [data writeToFile:path atomically:YES];
    
}

-(NSString *)nameForKey:(NSString *)key{

    NSString * name = [self.dic objectForKey:key];
    
    if (!name) {
        
        NSString *path = [self infoPathForKey:key];
    
        name = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
        if (name) {
            
            [self.dic setObject:name forKey:key];
            
        }else{
            NSLog(@"Error: unable to find %@", [self infoPathForKey:key]);
        }
        
    }
    return name;
    
}

-(void)setImage:(UIImage *)image forKey:(NSString *)key{
    
    [self.dic setObject:image forKey:key];
    
    NSString *path = [self infoPathForKey:key];
    
    NSData *data = UIImageJPEGRepresentation(image, 0.5);
   
    //NSData *data = UIImagePNGRepresentation(image);
    
    [data writeToFile:path atomically:YES];
}

-(UIImage *)imageForKey:(NSString *)key{
    
    UIImage *image = [self.dic objectForKey:key];
    if (!image) {
        NSString *path = [self infoPathForKey:key];
        image = [UIImage imageWithContentsOfFile:path];
        if (image) {
            [self.dic setObject:image forKey:key];
            
        }else{
            NSLog(@"Error: unable to find %@", [self infoPathForKey:key]);
        }
        
    }
    return image;
}

//获取沙河路径
-(NSString *)infoPathForKey:(NSString *)key{
    
    NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [documentDirectories firstObject];
    
    return [documentDirectory stringByAppendingPathComponent:key];
}

-(void)clearCaches:(NSNotification *)n{
    
    NSLog(@"Flushing %ld images out of the cache", (unsigned long)[self.dic count]);
    [self.dic removeAllObjects];
}



@end
